package co.yixiang.yshop.module.product.controller.admin.storeproductassure;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.product.controller.admin.storeproductassure.vo.*;
import co.yixiang.yshop.module.product.convert.storeproductassure.StoreProductAssureConvert;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductassure.StoreProductAssureDO;
import co.yixiang.yshop.module.product.service.storeproductassure.StoreProductAssureService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 商品保障服务")
@RestController
@RequestMapping("/yshop/store-product-assure")
@Validated
public class StoreProductAssureController {

    @Resource
    private StoreProductAssureService storeProductAssureService;

    @PostMapping("/create")
    @Operation(summary = "创建商品保障服务")
    @PreAuthorize("@ss.hasPermission('yshop:store-product-assure:create')")
    public CommonResult<Long> createStoreProductAssure(@Valid @RequestBody StoreProductAssureCreateReqVO createReqVO) {
        return success(storeProductAssureService.createStoreProductAssure(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品保障服务")
    @PreAuthorize("@ss.hasPermission('yshop:store-product-assure:update')")
    public CommonResult
            <Boolean> updateStoreProductAssure(@Valid @RequestBody StoreProductAssureUpdateReqVO
                                                       updateReqVO) {
        storeProductAssureService.updateStoreProductAssure(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品保障服务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yshop:store-product-assure:delete')")
    public CommonResult
            <Boolean> deleteStoreProductAssure(@RequestParam("id") Long id) {
        storeProductAssureService.deleteStoreProductAssure(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品保障服务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yshop:store-product-assure:query')")
    public CommonResult<StoreProductAssureRespVO> getStoreProductAssure
            (@RequestParam("id") Long id) {
        StoreProductAssureDO storeProductAssure = storeProductAssureService.getStoreProductAssure(id);
        return success(StoreProductAssureConvert.INSTANCE.convert(storeProductAssure));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商品保障服务列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('yshop:store-product-assure:query')")
    public CommonResult<List<StoreProductAssureRespVO>> getStoreProductAssureList(@RequestParam("ids")
                                                                                  Collection<Long> ids) {
        List<StoreProductAssureDO> list = storeProductAssureService.getStoreProductAssureList(ids);
        return success(StoreProductAssureConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品保障服务分页")
    @PreAuthorize("@ss.hasPermission('yshop:store-product-assure:query')")
    public CommonResult<PageResult<StoreProductAssureRespVO>> getStoreProductAssurePage(@Valid StoreProductAssurePageReqVO pageVO) {
        PageResult<StoreProductAssureDO> pageResult = storeProductAssureService.getStoreProductAssurePage(pageVO);
        return success(StoreProductAssureConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品保障服务 Excel")
    @PreAuthorize("@ss.hasPermission('yshop:store-product-assure:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreProductAssureExcel(@Valid StoreProductAssureExportReqVO exportReqVO,
                                              HttpServletResponse response) throws IOException {
        List<StoreProductAssureDO> list = storeProductAssureService.getStoreProductAssureList(exportReqVO);
        // 导出 Excel
        List<StoreProductAssureExcelVO> datas = StoreProductAssureConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品保障服务.xls",
                "数据", StoreProductAssureExcelVO.class, datas);
    }

}
